<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Command-module for Microsoft Outlook control &mdash; Dragonfly-modules v0.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Dragonfly-modules v0.1 documentation" href="index.html" />
    <link rel="next" title="Command-module for Foxit Reader PDF viewer" href="mod-_foxitreader.html" />
    <link rel="prev" title="Command-module for styles in Microsoft Word" href="mod-_word_styles.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_foxitreader.html" title="Command-module for Foxit Reader PDF viewer"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-_word_styles.html" title="Command-module for styles in Microsoft Word"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  
  <div class="section" id="command-module-for-microsoft-outlook-control">
<h1>Command-module for <strong>Microsoft Outlook</strong> control<a class="headerlink" href="#command-module-for-microsoft-outlook-control" title="Permalink to this headline">¶</a></h1>
<p>This module offers a number of commands for direct and efficient
control of Microsoft outlook.</p>
<div class="section" id="commands">
<h2>Commands<a class="headerlink" href="#commands" title="Permalink to this headline">¶</a></h2>
<dl class="docutils">
<dt>Command: <strong>&#8220;(folder | show me) &lt;folder&gt;&#8221;</strong></dt>
<dd>Jump straight to the specified folder.</dd>
<dt>Command: <strong>&#8220;move to &lt;folder&gt;&#8221;</strong></dt>
<dd>Move the selected item(s) to the specified folder.</dd>
<dt>Command: <strong>&#8220;save attachments&#8221;</strong></dt>
<dd>Save all attachments of selected items.</dd>
<dt>Command: <strong>&#8220;open attachment &lt;n&gt;&#8221;</strong></dt>
<dd>Open attachment number &lt;n&gt; within the selected item.</dd>
<dt>Command: <strong>&#8220;[create] new &lt;type&gt;&#8221;</strong></dt>
<dd>Creates a new item of the specified type.</dd>
<dt>Command: <strong>&#8220;(synchronize | update) (folders | folder list)&#8221;</strong></dt>
<dd>Update this module&#8217;s internal list of Outlook folders.</dd>
<dt>Command: <strong>&#8220;new (email | mail) [to &lt;addresses&gt;]&#8221;</strong></dt>
<dd>Create a new mail item.</dd>
<dt>Command: <strong>&#8220;forward [email | mail] [to &lt;addresses&gt;]&#8221;</strong></dt>
<dd>Forward to a selected mail item.</dd>
<dt>Commands for responding to a meeting request:</dt>
<dd><ul class="first last simple">
<li><strong>&#8220;accept and send&#8221;</strong></li>
<li><strong>&#8220;accept and edit&#8221;</strong></li>
<li><strong>&#8220;accept without response&#8221;</strong></li>
<li><strong>&#8220;decline and send&#8221;</strong></li>
<li><strong>&#8220;decline and edit&#8221;</strong></li>
<li><strong>&#8220;decline without response&#8221;</strong></li>
<li><strong>&#8220;tentative and send&#8221;</strong></li>
<li><strong>&#8220;tentative and edit&#8221;</strong></li>
<li><strong>&#8220;tentative without response&#8221;</strong></li>
<li><strong>&#8220;check calendar&#8221;</strong></li>
</ul>
</dd>
</dl>
</div>
<div class="section" id="configuration-examples">
<h2>Configuration examples<a class="headerlink" href="#configuration-examples" title="Permalink to this headline">¶</a></h2>
<div class="section" id="configuration-outlook-en-txt">
<h3>Configuration &#8220;_outlook-en.txt&#8221;<a class="headerlink" href="#configuration-outlook-en-txt" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># Dragonfly config for Microsoft Outlook control</span>
<span class="c">#</span>

<span class="c">#--- Language section ------------------------------------------------------</span>

<span class="n">lang</span><span class="o">.</span><span class="n">go_to_folder</span> <span class="o">=</span> <span class="s">&#39;(folder | show me) &lt;folder&gt;&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">save_attachments</span> <span class="o">=</span> <span class="s">&#39;save attachments&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">open_attachment</span> <span class="o">=</span> <span class="s">&#39;open attachment &lt;n&gt;&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">move_to_folder</span> <span class="o">=</span> <span class="s">&#39;move to &lt;folder&gt;&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">create_new_item</span> <span class="o">=</span> <span class="s">&#39;[create] new &lt;type&gt;&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">sync_folders</span> <span class="o">=</span> <span class="s">&#39;(synchronize | update) (folders | folder list)&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">item_type_mail</span> <span class="o">=</span> <span class="s">&#39;(mail | email)&#39;</span>
<span class="n">lang</span><span class="o">.</span><span class="n">item_type_task</span> <span class="o">=</span> <span class="s">&#39;task&#39;</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="module-source-code">
<h2>Module source code<a class="headerlink" href="#module-source-code" title="Permalink to this headline">¶</a></h2>
<p>The most current version of this module can be downloaded from the <a class="reference external" href="http://dragonfly-modules.googlecode.com/svn/trunk/command-modules/_outlook.py">repository here</a>.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># This file is a command-module for Dragonfly.</span>
<span class="c"># (c) Copyright 2008 by Christo Butcher</span>
<span class="c"># Licensed under the LGPL, see &lt;http://www.gnu.org/licenses/&gt;</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Command-module for **Microsoft Outlook** control</span>
<span class="sd">================================================</span>

<span class="sd">This module offers a number of commands for direct and efficient</span>
<span class="sd">control of Microsoft outlook.</span>

<span class="sd">Commands</span>
<span class="sd">--------</span>

<span class="sd">Command: **&quot;(folder | show me) &lt;folder&gt;&quot;**</span>
<span class="sd">    Jump straight to the specified folder.</span>

<span class="sd">Command: **&quot;move to &lt;folder&gt;&quot;**</span>
<span class="sd">    Move the selected item(s) to the specified folder.</span>

<span class="sd">Command: **&quot;save attachments&quot;**</span>
<span class="sd">    Save all attachments of selected items.</span>

<span class="sd">Command: **&quot;open attachment &lt;n&gt;&quot;**</span>
<span class="sd">    Open attachment number &lt;n&gt; within the selected item.</span>

<span class="sd">Command: **&quot;[create] new &lt;type&gt;&quot;**</span>
<span class="sd">    Creates a new item of the specified type.</span>

<span class="sd">Command: **&quot;(synchronize | update) (folders | folder list)&quot;**</span>
<span class="sd">    Update this module&#39;s internal list of Outlook folders.</span>

<span class="sd">Command: **&quot;new (email | mail) [to &lt;addresses&gt;]&quot;**</span>
<span class="sd">    Create a new mail item.</span>

<span class="sd">Command: **&quot;forward [email | mail] [to &lt;addresses&gt;]&quot;**</span>
<span class="sd">    Forward to a selected mail item.</span>

<span class="sd">Commands for responding to a meeting request:</span>
<span class="sd">     - **&quot;accept and send&quot;**</span>
<span class="sd">     - **&quot;accept and edit&quot;**</span>
<span class="sd">     - **&quot;accept without response&quot;**</span>
<span class="sd">     - **&quot;decline and send&quot;**</span>
<span class="sd">     - **&quot;decline and edit&quot;**</span>
<span class="sd">     - **&quot;decline without response&quot;**</span>
<span class="sd">     - **&quot;tentative and send&quot;**</span>
<span class="sd">     - **&quot;tentative and edit&quot;**</span>
<span class="sd">     - **&quot;tentative without response&quot;**</span>
<span class="sd">     - **&quot;check calendar&quot;**</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">pkg_resources</span>
<span class="n">pkg_resources</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="s">&quot;dragonfly &gt;= 0.6.5beta1.dev-r76&quot;</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">from</span> <span class="nn">win32com.client</span>  <span class="kn">import</span> <span class="n">constants</span><span class="p">,</span> <span class="n">gencache</span>
<span class="kn">from</span> <span class="nn">pywintypes</span>       <span class="kn">import</span> <span class="n">com_error</span>

<span class="kn">from</span> <span class="nn">dragonfly</span> <span class="kn">import</span> <span class="p">(</span><span class="n">ConnectionGrammar</span><span class="p">,</span> <span class="n">AppContext</span><span class="p">,</span> <span class="n">DictListRef</span><span class="p">,</span> <span class="n">Choice</span><span class="p">,</span>
                       <span class="n">CompoundRule</span><span class="p">,</span> <span class="n">MappingRule</span><span class="p">,</span> <span class="n">DictList</span><span class="p">,</span> <span class="n">RuleRef</span><span class="p">,</span>
                       <span class="n">Config</span><span class="p">,</span> <span class="n">Section</span><span class="p">,</span> <span class="n">Item</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">Key</span><span class="p">,</span> <span class="n">Text</span><span class="p">,</span> <span class="n">Pause</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Set up this module&#39;s configuration.</span>

<span class="n">config</span> <span class="o">=</span> <span class="n">Config</span><span class="p">(</span><span class="s">&quot;Outlook control&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span>                 <span class="o">=</span> <span class="n">Section</span><span class="p">(</span><span class="s">&quot;Language section&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">go_to_folder</span>    <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;(folder | show me) &lt;folder&gt;&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">move_to_folder</span>  <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;move to &lt;folder&gt;&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">save_attachments</span> <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;save (attachments | edges)&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">open_attachment</span> <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;open (attachment | edge) &lt;n&gt;&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">create_new_item</span> <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;[create] new &lt;type&gt;&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">sync_folders</span>    <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;(synchronize | update) (folders | folder list)&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">item_type_mail</span>  <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;(mail | email)&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">item_type_task</span>  <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;task&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">new_email</span>       <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;new (email | mail) [to &lt;addresses&gt;]&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">forward_email</span>   <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;forward [email | mail] [to &lt;addresses&gt;]&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">address_and_word</span> <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;[and]&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">meeting_request_actions</span> <span class="o">=</span> <span class="n">Item</span><span class="p">({</span>
      <span class="s">&quot;accept and send&quot;</span><span class="p">:</span>             <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, c/20, s/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;accept and edit&quot;</span><span class="p">:</span>             <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, c/20, e/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;accept without response&quot;</span><span class="p">:</span>     <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, c/20, d/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;decline and send&quot;</span><span class="p">:</span>            <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, e/20, s/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;decline and edit&quot;</span><span class="p">:</span>            <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, e/20, e/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;decline without response&quot;</span><span class="p">:</span>    <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, e/20, d/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;tentative and send&quot;</span><span class="p">:</span>          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, a/20, s/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;tentative and edit&quot;</span><span class="p">:</span>          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, a/20, e/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;tentative without response&quot;</span><span class="p">:</span>  <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, a/20, d/10, enter&quot;</span><span class="p">),</span>
      <span class="s">&quot;check calendar&quot;</span><span class="p">:</span>              <span class="n">Key</span><span class="p">(</span><span class="s">&quot;a-a/10, h&quot;</span><span class="p">),</span>
     <span class="p">},</span> <span class="n">namespace</span><span class="o">=</span><span class="p">{</span><span class="s">&quot;Key&quot;</span><span class="p">:</span> <span class="n">Key</span><span class="p">})</span>
<span class="n">config</span><span class="o">.</span><span class="n">contacts</span>             <span class="o">=</span> <span class="n">Section</span><span class="p">(</span><span class="s">&quot;Contacts section&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">contacts</span><span class="o">.</span><span class="n">addresses</span>   <span class="o">=</span> <span class="n">Item</span><span class="p">({</span>
      <span class="s">&quot;someone&quot;</span><span class="p">:</span> <span class="s">&quot;someone@example.com&quot;</span><span class="p">,</span>
     <span class="p">})</span>
<span class="c">#config.generate_config_file()</span>
<span class="n">config</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Utility generator function for iterating over COM collections.</span>

<span class="k">def</span> <span class="nf">collection_iter</span><span class="p">(</span><span class="n">collection</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span> <span class="n">collection</span><span class="o">.</span><span class="n">Count</span> <span class="o">+</span> <span class="mf">1</span><span class="p">):</span>
        <span class="k">yield</span> <span class="n">collection</span><span class="o">.</span><span class="n">Item</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>

<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># This module&#39;s main grammar.</span>

<span class="k">class</span> <span class="nc">ControlGrammar</span><span class="p">(</span><span class="n">ConnectionGrammar</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">folders</span> <span class="o">=</span> <span class="n">DictList</span><span class="p">(</span><span class="s">&quot;folders&quot;</span><span class="p">)</span>
        <span class="n">gencache</span><span class="o">.</span><span class="n">EnsureDispatch</span><span class="p">(</span><span class="s">&quot;Outlook.Application&quot;</span><span class="p">)</span>
        <span class="n">ConnectionGrammar</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">name</span><span class="o">=</span><span class="s">&quot;outlook control&quot;</span><span class="p">,</span>
            <span class="n">context</span><span class="o">=</span><span class="n">AppContext</span><span class="p">(</span><span class="n">executable</span><span class="o">=</span><span class="s">&quot;outlook&quot;</span><span class="p">),</span>
            <span class="n">app_name</span><span class="o">=</span><span class="s">&quot;Outlook.Application&quot;</span>
           <span class="p">)</span>

    <span class="k">def</span> <span class="nf">connection_up</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Made connection with Outlook -&gt; retrieves available folders.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">update_folders</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">connection_down</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Lost connection with Outlook -&gt; empty folders list.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset_folders</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">update_folders</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">namespace</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">GetNamespace</span><span class="p">(</span><span class="s">&quot;MAPI&quot;</span><span class="p">)</span>
        <span class="n">inbox_folder</span> <span class="o">=</span> <span class="n">namespace</span><span class="o">.</span><span class="n">GetDefaultFolder</span><span class="p">(</span><span class="n">constants</span><span class="o">.</span><span class="n">olFolderInbox</span><span class="p">)</span>
        <span class="n">root_folder</span> <span class="o">=</span> <span class="n">inbox_folder</span><span class="o">.</span><span class="n">Parent</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">folders</span><span class="o">.</span><span class="n">set</span><span class="p">({})</span>
        <span class="n">stack</span> <span class="o">=</span> <span class="p">[</span><span class="n">collection_iter</span><span class="p">(</span><span class="n">root_folder</span><span class="o">.</span><span class="n">Folders</span><span class="p">)]</span>
        <span class="k">while</span> <span class="n">stack</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">folder</span> <span class="o">=</span> <span class="n">stack</span><span class="p">[</span><span class="o">-</span><span class="mf">1</span><span class="p">]</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
                <span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
                <span class="k">continue</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">folders</span><span class="p">[</span><span class="n">folder</span><span class="o">.</span><span class="n">Name</span><span class="p">]</span> <span class="o">=</span> <span class="n">folder</span>
            <span class="n">stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">collection_iter</span><span class="p">(</span><span class="n">folder</span><span class="o">.</span><span class="n">Folders</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">reset_folders</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">folders</span><span class="o">.</span><span class="n">set</span><span class="p">({})</span>

    <span class="k">def</span> <span class="nf">get_active_explorer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">ActiveExplorer</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">com_error</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: COM error getting active explorer: </span><span class="si">%s</span><span class="s">&quot;</span>
                              <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
            <span class="k">return</span> <span class="bp">None</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: no active explorer.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">return</span> <span class="n">explorer</span>

<span class="n">grammar</span> <span class="o">=</span> <span class="n">ControlGrammar</span><span class="p">()</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Synchronize folders rule for explicitly updating folder list.</span>

<span class="k">class</span> <span class="nc">SynchronizeFoldersRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>
    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">sync_folders</span>
    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">update_folders</span><span class="p">()</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">SynchronizeFoldersRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">GoToFolderRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">go_to_folder</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">DictListRef</span><span class="p">(</span><span class="s">&quot;folder&quot;</span><span class="p">,</span> <span class="n">grammar</span><span class="o">.</span><span class="n">folders</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">folder</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;folder&quot;</span><span class="p">]</span>

        <span class="c"># Get the currently active explorer.</span>
        <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span> <span class="k">return</span>

        <span class="c"># Make the explorer view the given folder.</span>
        <span class="n">explorer</span><span class="o">.</span><span class="n">SelectFolder</span><span class="p">(</span><span class="n">folder</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">GoToFolderRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">MoveToFolderRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">move_to_folder</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">DictListRef</span><span class="p">(</span><span class="s">&quot;folder&quot;</span><span class="p">,</span> <span class="n">grammar</span><span class="o">.</span><span class="n">folders</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">folder</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;folder&quot;</span><span class="p">]</span>

        <span class="c"># Get the currently active explorer.</span>
        <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span> <span class="k">return</span>

        <span class="c"># Move the selected items to the given folder.</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: moving item </span><span class="si">%r</span><span class="s"> to folder </span><span class="si">%r</span><span class="s">.&quot;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">Subject</span><span class="p">,</span> <span class="n">folder</span><span class="o">.</span><span class="n">Name</span><span class="p">))</span>
            <span class="n">item</span><span class="o">.</span><span class="n">Move</span><span class="p">(</span><span class="n">folder</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">MoveToFolderRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">SaveAttachmentsRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">save_attachments</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>

        <span class="c"># Get the currently active explorer.</span>
        <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span> <span class="k">return</span>

        <span class="c"># Save the attachments of the selected items.</span>
        <span class="n">temp_dir</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;temporary directory:&quot;</span><span class="p">,</span> <span class="n">temp_dir</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: saving attachments of item </span><span class="si">%r</span><span class="s">.&quot;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">Subject</span><span class="p">))</span>

            <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">Attachments</span><span class="p">):</span>
                <span class="k">print</span> <span class="s">&quot;attachment </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">attachment</span><span class="o">.</span><span class="n">FileName</span>
                <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">attachment</span><span class="o">.</span><span class="n">FileName</span><span class="p">)</span>
                <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">temp_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
                <span class="n">attachment</span><span class="o">.</span><span class="n">SaveAsFile</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

        <span class="c"># Open a file browser to the containing directory.</span>
        <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">([</span><span class="s">&quot;explorer&quot;</span><span class="p">,</span> <span class="n">temp_dir</span><span class="p">])</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">SaveAttachmentsRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">OpenAttachmentRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">open_attachment</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">Integer</span><span class="p">(</span><span class="s">&quot;n&quot;</span><span class="p">,</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">10</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;n&quot;</span><span class="p">]</span>

        <span class="c"># Get the currently active explorer.</span>
        <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span> <span class="k">return</span>

        <span class="c"># Make sure that exactly 1 item is selected.</span>
        <span class="k">if</span> <span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="o">.</span><span class="n">Count</span> <span class="o">&lt;</span> <span class="mf">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: no selected, not opening.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">elif</span> <span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="o">.</span><span class="n">Count</span> <span class="o">&gt;</span> <span class="mf">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: multiple items selected, not opening.&quot;</span>
                              <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="c"># Retrieve the attachment of the selected item.</span>
        <span class="n">item</span> <span class="o">=</span> <span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="o">.</span><span class="n">Item</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="mf">1</span> <span class="o">&lt;=</span> <span class="n">index</span> <span class="o">&lt;=</span> <span class="n">item</span><span class="o">.</span><span class="n">Attachments</span><span class="o">.</span><span class="n">Count</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: attachment index </span><span class="si">%d</span><span class="s"> of item </span><span class="si">%r</span><span class="s">&quot;</span>
                              <span class="s">&quot; out of range (1 &lt;= index &lt;= </span><span class="si">%d</span><span class="s">).&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">Subject</span><span class="p">,</span>
                           <span class="n">item</span><span class="o">.</span><span class="n">Attachments</span><span class="o">.</span><span class="n">Count</span><span class="p">))</span>
            <span class="k">return</span>

        <span class="n">attachment</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">Attachments</span><span class="o">.</span><span class="n">Item</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: opening attachment </span><span class="si">%r</span><span class="s"> of item </span><span class="si">%r</span><span class="s">.&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attachment</span><span class="o">.</span><span class="n">FileName</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">Subject</span><span class="p">))</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">attachment</span><span class="o">.</span><span class="n">FileName</span><span class="p">)</span>

        <span class="c"># Save the attachment to a temporary directory.</span>
        <span class="n">temp_dir</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">temp_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
        <span class="n">attachment</span><span class="o">.</span><span class="n">SaveAsFile</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

        <span class="c"># Open a file browser to the containing directory.</span>
        <span class="n">os</span><span class="o">.</span><span class="n">startfile</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">OpenAttachmentRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">MeetingRequestRule</span><span class="p">(</span><span class="n">MappingRule</span><span class="p">):</span>

    <span class="n">mapping</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">meeting_request_actions</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>

        <span class="c"># Get the currently active explorer.</span>
        <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span> <span class="k">return</span>

        <span class="c"># Save the attachments of the selected items.</span>
        <span class="k">if</span> <span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="o">.</span><span class="n">Count</span> <span class="o">!=</span> <span class="mf">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: cannot accept meeting requests when&quot;</span>
                              <span class="s">&quot; multiple items are selected.&quot;</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="c"># Retrieve the item.</span>
        <span class="n">item</span> <span class="o">=</span> <span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="o">.</span><span class="n">Item</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span>

        <span class="c"># Perform spoken action on this meeting request.</span>
        <span class="n">value</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">MeetingRequestRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Simple new item rule.</span>

<span class="k">class</span> <span class="nc">NewItemRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">create_new_item</span>
    <span class="n">types</span> <span class="o">=</span> <span class="p">{</span>        <span class="c"># Use delayed constant lookups, because these</span>
                     <span class="c">#  are loaded when the com connection is set up.</span>
             <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">item_type_task</span><span class="p">:</span>    <span class="s">&quot;olTaskItem&quot;</span><span class="p">,</span>
            <span class="p">}</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">Choice</span><span class="p">(</span><span class="s">&quot;type&quot;</span><span class="p">,</span> <span class="n">types</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">item_type</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">constants</span><span class="p">,</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;type&quot;</span><span class="p">])</span>
        <span class="n">item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">CreateItem</span><span class="p">(</span><span class="n">item_type</span><span class="p">)</span>
        <span class="n">item</span><span class="o">.</span><span class="n">Display</span><span class="p">()</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">NewItemRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Address list rule and element.</span>

<span class="k">class</span> <span class="nc">AddressListRule</span><span class="p">(</span><span class="n">MappingRule</span><span class="p">):</span>

    <span class="n">exported</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">mapping</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">contacts</span><span class="o">.</span><span class="n">addresses</span>


<span class="k">class</span> <span class="nc">Addresses</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">exported</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">max_names</span> <span class="o">=</span> <span class="mf">3</span>
    <span class="n">spec</span> <span class="o">=</span> <span class="s">&quot;&lt;name&gt;&quot;</span> \
           <span class="o">+</span> <span class="p">(</span><span class="s">&quot; [&quot;</span> <span class="o">+</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">address_and_word</span> <span class="o">+</span> <span class="s">&quot; &lt;name&gt;&quot;</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">max_names</span><span class="o">-</span><span class="mf">1</span><span class="p">)</span> \
           <span class="o">+</span> <span class="s">&quot;]&quot;</span> <span class="o">*</span> <span class="p">(</span><span class="n">max_names</span><span class="o">-</span><span class="mf">1</span><span class="p">)</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">RuleRef</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="n">rule</span><span class="o">=</span><span class="n">AddressListRule</span><span class="p">())]</span>

    <span class="k">def</span> <span class="nf">value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="n">children</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_children_by_name</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">child</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">children</span><span class="p">]</span>

<span class="n">addresses</span> <span class="o">=</span> <span class="n">RuleRef</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;addresses&quot;</span><span class="p">,</span> <span class="n">rule</span><span class="o">=</span><span class="n">Addresses</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Voice command for creating new (addressed) e-mails.</span>

<span class="k">class</span> <span class="nc">NewMailRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">new_email</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">addresses</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">item_type</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">constants</span><span class="p">,</span> <span class="s">&quot;olMailItem&quot;</span><span class="p">)</span>
        <span class="n">item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">CreateItem</span><span class="p">(</span><span class="n">item_type</span><span class="p">)</span>
        <span class="n">item</span><span class="o">.</span><span class="n">Display</span><span class="p">()</span>

        <span class="k">if</span> <span class="s">&quot;addresses&quot;</span> <span class="ow">in</span> <span class="n">extras</span><span class="p">:</span>
            <span class="n">addresses</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;addresses&quot;</span><span class="p">]</span>
            <span class="n">action</span> <span class="o">=</span> <span class="n">Pause</span><span class="p">(</span><span class="s">&quot;100&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Text</span><span class="p">(</span><span class="s">&quot;; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">addresses</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;;&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;tab:2&quot;</span><span class="p">)</span>
            <span class="n">action</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">NewMailRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Voice command for forwarding selected mail items.</span>

<span class="k">class</span> <span class="nc">ForwardRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">forward_email</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">addresses</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="c"># Get the currently active explorer.</span>
        <span class="n">explorer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">explorer</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="c"># Forward to first item of the current selection.</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">explorer</span><span class="o">.</span><span class="n">Selection</span><span class="p">):</span>
            <span class="c"># Create a forwarded copy and display it.</span>
            <span class="n">message</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">Forward</span><span class="p">()</span>
            <span class="n">message</span><span class="o">.</span><span class="n">Display</span><span class="p">()</span>

            <span class="c"># Insert addresses if given.</span>
            <span class="k">if</span> <span class="s">&quot;addresses&quot;</span> <span class="ow">in</span> <span class="n">extras</span><span class="p">:</span>
                <span class="n">addresses</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;addresses&quot;</span><span class="p">]</span>
                <span class="n">action</span> <span class="o">=</span> <span class="n">Pause</span><span class="p">(</span><span class="s">&quot;100&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Text</span><span class="p">(</span><span class="s">&quot;; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">addresses</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;;&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;tab:3&quot;</span><span class="p">)</span>
                <span class="n">action</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

            <span class="c"># Only handle the first selected item.</span>
            <span class="k">break</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">ForwardRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">RetrieveContactsRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="s">&quot;retrieve Outlook contacts&quot;</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">namespace</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">GetNamespace</span><span class="p">(</span><span class="s">&quot;MAPI&quot;</span><span class="p">)</span>
        <span class="n">addresses</span> <span class="o">=</span> <span class="n">namespace</span><span class="o">.</span><span class="n">AddressLists</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">address_list</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">addresses</span><span class="p">):</span>
            <span class="k">print</span> <span class="s">&quot;address list:&quot;</span><span class="p">,</span> <span class="n">address_list</span><span class="o">.</span><span class="n">Name</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">address_list</span><span class="o">.</span><span class="n">AddressEntries</span><span class="p">):</span>
                <span class="k">print</span> <span class="s">&quot;    entry name: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">entry</span><span class="o">.</span><span class="n">Name</span>
                <span class="n">entries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
        <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">Name</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">]</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;    </span><span class="si">%(name)20r</span><span class="s">: </span><span class="si">%(name)r</span><span class="s">,&quot;</span> <span class="o">%</span> <span class="p">{</span><span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="n">n</span><span class="p">}</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">names</span><span class="p">]</span>
        <span class="n">lines</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span> <span class="s">&quot;contacts.names = {&quot;</span><span class="p">)</span>
        <span class="n">lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;    }&quot;</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>
        <span class="k">print</span> <span class="n">output</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">RetrieveContactsRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Load the grammar instance and define how to unload it.</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>

<span class="c"># Unload function which will be called by natlink at unload time.</span>
<span class="k">def</span> <span class="nf">unload</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">grammar</span>
    <span class="k">if</span> <span class="n">grammar</span><span class="p">:</span> <span class="n">grammar</span><span class="o">.</span><span class="n">unload</span><span class="p">()</span>
    <span class="n">grammar</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Command-module for <strong>Microsoft Outlook</strong> control</a><ul>
<li><a class="reference external" href="#commands">Commands</a></li>
<li><a class="reference external" href="#configuration-examples">Configuration examples</a><ul>
<li><a class="reference external" href="#configuration-outlook-en-txt">Configuration &#8220;_outlook-en.txt&#8221;</a></li>
</ul>
</li>
<li><a class="reference external" href="#module-source-code">Module source code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="mod-_word_styles.html" title="previous chapter">Command-module for styles in <strong>Microsoft Word</strong></a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mod-_foxitreader.html" title="next chapter">Command-module for <strong>Foxit Reader</strong> PDF viewer</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/mod-_outlook.txt">Show Source</a></li>
            </ul>
          <h3>Quick search</h3>
            <form class="search" action="search.html" method="get">
              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
              <input type="hidden" name="check_keywords" value="yes" />
              <input type="hidden" name="area" value="default" />
            </form>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_foxitreader.html" title="Command-module for Foxit Reader PDF viewer"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-_word_styles.html" title="Command-module for styles in Microsoft Word"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Christo Butcher.
      Last updated on Apr 10, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.1.
    </div>
  </body>
</html>